package com.winchain.chainbill.bill.biz.send.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.ParameterHelper;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.vo.ParameterVo;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.basic.cus.service.CusRelService;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.biz.send.service.BillImptService;
import com.winchain.chainbill.bill.biz.send.service.BillService;
import com.winchain.chainbill.bill.biz.send.vo.BillImpt;
import com.winchain.chainbill.bill.biz.send.vo.ReadEcho;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("billImptService")
public class BillImptServiceImpl implements BillImptService {
	@Autowired
	private CusRecService basCusService;
	@Autowired
	private BillService billService;
	@Autowired
	private CusRelService cusRelService;
	private static Pattern BILL_MONEY_PATTERN = Pattern.compile("^([1-9]+\\d*)|([0-9]+\\d*\\.\\d{1,2})$");
	private static Pattern BILL_DATE_PATTERN = Pattern.compile("^\\d+$");
	private static Pattern IS_EIGHT_NUMBER = Pattern.compile("^\\d{8}$");

	public ReadEcho checkBillImpt(List<BillImpt> list, String finId, String grpId) {
		ReadEcho echo = new ReadEcho();
		echo.setData(list);
		StringBuilder builder = new StringBuilder();
		int counter = 1;

		String billAmt = "";
		String sendDate = "";
		String expireDate = "";
		String term = "";
		for (BillImpt bill : list) {
			builder.append(checkCus(bill.getSellerNm(), counter, finId));

			billAmt = bill.getBillAmt();
			if ((StringUtil.emptyAndNull(billAmt)) || (!BILL_MONEY_PATTERN.matcher(billAmt).matches())) {
				builder.append("��").append(counter).append("��: ����������������������;").append("$");
			}
			sendDate = bill.getSndDate();
			if (StringUtil.emptyAndNull(sendDate)) {
				builder.append("��").append(counter).append("��: ����������������������;").append("$");
			}
			expireDate = bill.getExpDate();
			if (StringUtil.emptyAndNull(expireDate)) {
				builder.append("��").append(counter).append("��: ����������������������;").append("$");
			}
			term = bill.getTerm();
			if ((StringUtil.emptyAndNull(term))
					|| (!BILL_DATE_PATTERN.matcher(term.replaceAll("\\.0", "")).matches())) {
				builder.append("��").append(counter).append("��: ����������������������").append("$");
			} else if (!StringUtil.emptyAndNull(grpId)) {
				ParameterVo sndLimtDate = ParameterHelper.getParameter("6", "BILL_SEND_LIM_DATE", grpId);
				int maxLimtDate = 90;
				if ((sndLimtDate != null) && (!StringUtil.empty(sndLimtDate.getParmValue()))) {
					maxLimtDate = Integer.parseInt(sndLimtDate.getParmValue());
				}
				if (maxLimtDate < Integer.parseInt(term)) {
					builder.append("��").append(counter).append("��: ��������������������[" + maxLimtDate + "��]")
							.append("$");
				}
			}
			if ((!StringUtil.emptyAndNull(sendDate)) && (!StringUtil.emptyAndNull(expireDate))) {
				int dateResut = checkSendDate(sendDate, expireDate);
				if (dateResut == 0) {
					builder.append("��").append(counter).append("��: ��������������������������").append("$");
				} else if (dateResut == 1) {
					builder.append("��").append(counter).append("��: ��������������������").append("$");
				}
			}
			bill.setBailRatio("0");
			if (StringUtil.emptyAndNull(builder.toString())) {
				echo.addAmount(new BigDecimal(billAmt));
			}
			counter++;
		}
		echo.setError(builder.toString());

		return echo;
	}

	private int checkSendDate(String sendDate, String expireDate) {
		try {
			Date sDate = SysDateUtil.getDate(sendDate, "yyyyMMdd");
			Date eDate = SysDateUtil.getDate(expireDate, "yyyyMMdd");
			if (sDate.equals(eDate)) {
				return 0;
			}
			if (sDate.after(eDate)) {
				return 1;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 3;
	}

	private String checkCus(String cusName, int line, String finId) {
		StringBuffer error = new StringBuffer();
		if (StringUtil.empty(cusName)) {
			error.append("��").append(line).append("��: ����������������;").append("$");
		} else {
			CusRec cus = new CusRec();
			cus.setCusName(cusName);
			cus.setCusState("E");
			cus = (CusRec) this.basCusService.selectOne(cus);
			if (null == cus) {
				error.append("��").append(line).append("��: ��������[").append(cusName).append("]").append("������;")
						.append("$");
			} else if (cus.getCusId().equals(CommonDataUtil.getCurEntId())) {
				error.append("��").append(line).append("��: ��������[").append(cusName).append("]")
						.append("����������������;").append("$");
			} else if (!this.cusRelService.checkCusRel(cus.getCusId(), CommonDataUtil.getCurEntId(), "1")) {
				error.append("��").append(line).append("��: ��������[").append(cusName).append("]")
						.append("������������������").append("$");
			}
		}
		return error.toString();
	}

	public CbRec getCbRecord(BillImpt impt) {
		CbRec record = new CbRec();

		String billAmt = impt.getBillAmt();
		String sendDate = impt.getSndDate();
		String expireDate = impt.getExpDate();

		record.setBillAmt(new BigDecimal(billAmt));
		record.setBillType("1");
		record.setSndDate(sendDate);
		record.setExpDate(expireDate);
		record.setBillCode(this.billService.getBillCode(CommonDataUtil.getCurEntId(), "R"));

		return record;
	}

	public CbSnd getCbSnd(BillImpt impt) {
		CbSnd send = new CbSnd();

		String sellerName = impt.getSellerNm();
		String bailRatio = impt.getBailRatio();

		CusRec seller = new CusRec();
		seller.setCusName(sellerName);
		seller = (CusRec) this.basCusService.selectOne(seller);
		if (seller == null) {
			throw new RuntimeException("����������:[" + sellerName + "]��������������������������������");
		}
		if (StringUtil.notEmpty(bailRatio)) {
			send.setBailRatio(new BigDecimal(bailRatio));
		}
		send.setBuyerId(CommonDataUtil.getCurEntId());
		send.setBuyerNm(CommonDataUtil.getCurEntName());

		send.setSellerId(seller.getCusId());
		send.setSellerNm(sellerName);
		send.setContractNo(impt.getContractNo());
		send.setInvoiceNo(impt.getInvoiceNo());

		return send;
	}
}
